package org.apache.shiro.session.mgt;

import java.util.Date;

import org.apache.shiro.subject.SimplePrincipalCollection;
import org.kl.bf.service.basic.ShiroDbRealm.ShiroUser;

import com.fasterxml.jackson.annotation.JsonFormat;

public class SessionGrid {
	private String sessionId;
	private String userId;// 登录者ID
	private String trueName;// 登录者姓名
	private String lastName;// 上一个身份的姓名
	private String isRunas;// 是否身份委托
	private String sysManager;// 是否管理员
	private String loginName;// 登录者账号
	private String deptName;// 登录者所在部门名称
	private String deptId;// 登录者所在部门Id
	private String host;// 用户主机IP
	private String userAgent;// 用户浏览器信息
	private String systemHost;// 当前系统主机地址
	private Date startTime;// 登录时间
	private Date stopTime;// 登出时间
	private Date lastAccessTime;// 上次访问时间
	private String onlineStatus;// 在线状态

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	public String getSystemHost() {
		return systemHost;
	}

	public void setSystemHost(String systemHost) {
		this.systemHost = systemHost;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getStopTime() {
		return stopTime;
	}

	public void setStopTime(Date stopTime) {
		this.stopTime = stopTime;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getLastAccessTime() {
		return lastAccessTime;
	}

	public void setLastAccessTime(Date lastAccessTime) {
		this.lastAccessTime = lastAccessTime;
	}

	public String getOnlineStatus() {
		return onlineStatus;
	}

	public void setOnlineStatus(String onlineStatus) {
		this.onlineStatus = onlineStatus;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getIsRunas() {
		return isRunas;
	}

	public void setIsRunas(String isRunas) {
		this.isRunas = isRunas;
	}

	public String getSysManager() {
		return sysManager;
	}

	public void setSysManager(String sysManager) {
		this.sysManager = sysManager;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public static final SessionGrid fromOnlineSession(OnlineSession session) {
		if (session != null) {
			SessionGrid sg = new SessionGrid();
			Boolean isRunas = (Boolean) session.getAttribute("isRunas");
			sg.setIsRunas(isRunas != null && isRunas ? "是" : "否");
			SimplePrincipalCollection spc = (SimplePrincipalCollection) session
					.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY");
			if (spc != null) {
				ShiroUser su = (ShiroUser) spc.getPrimaryPrincipal();
				if (su != null) {
					sg.setDeptId(su.getDeptId());
					sg.setDeptName(su.getDeptName());
					sg.setLoginName(su.getLoginName());
					sg.setTrueName(su.getName());
					sg.setUserId(su.getId());
					sg.setSysManager(su.getSysManager() != null && su.getSysManager() == 1 ? "是" : "否");
				}
			}
			sg.setHost(session.getHost());
			sg.setLastAccessTime(session.getLastAccessTime());
			sg.setOnlineStatus(session.getStatus().getInfo());
			sg.setSessionId((String) session.getId());
			sg.setStartTime(session.getStartTimestamp());
			sg.setStopTime(session.getStopTimestamp());
			sg.setSystemHost(session.getSystemHost());
			sg.setUserAgent(session.getUserAgent());
			return sg;
		}
		return null;
	}
}